
const express = require('express');
const { addBlogService, updateBlogService, delBlogService, getBlogByIdService, getBlogByPageService } = require('../service/blogService');
const { formatResponse } = require('../utils/tool');

const router = express.Router();

router.post('/', async function (req, res, next) {
  let result = await addBlogService(req.body)
  res.send(formatResponse(0, "新增成功", result))
});

router.put('/:id', async function (req, res, next) {
  let result = await updateBlogService(req.params.id, req.body)
  res.send(formatResponse(0, "修改成功", result))
})

router.delete('/:id', async function (req, res, next) {
  let id = req.params.id
  await delBlogService(id)
  res.send(formatResponse(0, "删除成功"))
})

router.get('/:id', async function (req, res, next) {
  let id = req.params.id
  let blog = await getBlogByIdService(id)
  res.send(formatResponse(0, "获取成功", blog))
});

router.get('/', async function (req, res, next) {
  let query = {
    page: parseInt(req.query.page) || 1,
    limit: parseInt(req.query.limit) || 10,
    keyword: req.query.keyword || "",
    categoryId: req.query.categoryId || -1
  }

  let blogList = await getBlogByPageService(query)
  res.send(formatResponse(0, "获取成功", blogList))
})

module.exports = router;

